<template>
  <el-dialog
    width="600px"
    title="认证资料"
    :visible.sync="dialogVisible"
  >
    <div class="flex idcard_wrap">
      <div class="flex-1">
        <p class="text-center img_header">身份证正面</p>
        <div class="img_wrap">
          <img
            v-if="idcard_front"
            :src="$globalObj.imgBaseUrl+idcard_front"
            class="w-100 h-100"
          >
          <el-image-viewer
            v-if="showFront"
            :on-close="closeFront"
            :url-list="[$globalObj.imgBaseUrl+idcard_front]"
          />
        </div>
        <p class="text-center img_footer">
          <el-button
            size="mini"
            type="primary"
            @click="idcard_front&&(showFront=true)"
          >查看大图</el-button>
          <el-upload
            action="string"
            style="display:inline-block;margin-left:10px"
            :show-file-list="false"
            :http-request="httpRequest_front"
            :before-upload="beforeUpload"
          >
            <el-button
              size="mini"
              type="primary"
            >上传</el-button>
          </el-upload>
        </p>
      </div>
      <div class="flex-1">
        <p class="text-center img_header">身份证反面</p>
        <div class="img_wrap">
          <img
            v-if="idcard_back"
            :src="$globalObj.imgBaseUrl+idcard_back"
            class="w-100 h-100"
          >
          <el-image-viewer
            v-if="showBack"
            :on-close="closeBack"
            :url-list="[$globalObj.imgBaseUrl+idcard_back]"
          />
        </div>
        <p class="text-center img_footer">
          <el-button
            size="mini"
            type="primary"
            @click="idcard_back&&(showBack=true)"
          >查看大图</el-button>
          <el-upload
            action="string"
            style="display:inline-block;margin-left:10px"
            :show-file-list="false"
            :http-request="httpRequest_back"
            :before-upload="beforeUpload"
          >
            <el-button
              size="mini"
              type="primary"
            >上传</el-button>
          </el-upload>
        </p>
      </div>
    </div>
    <el-form :inline="true" ref="identityForm"
             :model="identityForm"
             class="bbb"
             label-width="140px">
      <el-form-item label="姓名">
        <el-input v-model="identityForm.formname" id="n1"></el-input>
      </el-form-item>
      <el-form-item label="身份证号码">
        <el-input v-model="identityForm.idNumber"></el-input>
      </el-form-item>
    </el-form>
    <!-- <div>
      <p class="text-center img_header">用户手机号</p>
      <el-input
        v-model="mobile"
        placeholder="请输入电话"
        type="text"
        maxlength="50"
      />
      <el-button
        type="primary"
        @click="changeMemberMobile"
      >修改用户手机号</el-button>
    </div> -->
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        type="primary"
        @click="dialogVisible=false"
      >关闭</el-button>
      <el-button
        type="primary"
        @click="save"
      >保存修改</el-button>
    </div>
  </el-dialog>
</template>

<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
import { changeMemberMobile, changeMemberNoCard } from '.././../../api/order'
import { upload } from '@/api/index'
export default {
  components: { ElImageViewer },
  props: ['editItem', 'getList'],
  data() {
    return {
      dialogVisible: false,
      showFront: false,
      showBack: false,
      idcard_back: '',
      idcard_front: '',
      mobile: '',
      isChange: false,
      identityForm:{
        formname:'',
        idNumber:''
      }
    }
  },
  watch: {
    dialogVisible(val) {
      this.isChange = false
      if (val) {
        this.idcard_back = this.editItem.idcard_back
        this.idcard_front = this.editItem.idcard_front
        this.mobile = this.editItem.address_mobile
        this.identityForm.formname = this.editItem.formname
        this.identityForm.idNumber = this.editItem.idNumber

      } else {
        this.idcard_back = ''
        this.idcard_front = ''
        this.mobile = ''
      }
    }
  },
  methods: {
    save() {
      changeMemberNoCard({ order_uid: this.editItem.uid, front: this.idcard_front, back: this.idcard_back,
        formName:this.identityForm.formname,idNumber: this.identityForm.idNumber}).then(res => {
        if (res) {
          this.$message.success('修改成功')
          console.log("90909",res)
          this.$emit('getList', true)
          this.dialogVisible = false
        }
      })
    },
    // 上传前对文件的大小进行判断
    beforeUpload(file) {
      this.fileInfo = file
      let isFileSize = true
      const hasAccept = true
      let isSize = true
      if (this.fileSize) {
        const size = parseInt(this.fileSize.slice(0, -1))
        if (this.fileSize.indexOf('K') > -1) {
          isFileSize = file.size / 1024 < size
        }
        if (this.fileSize.indexOf('M') > -1) {
          isFileSize = file.size / 1024 / 1024 < size
        }
        if (!isFileSize) {
          this.$message({
            message: '上传文件大小不能超过' + this.fileSize,
            type: 'error'
          })
          return false
        }
      }
      if (this.accept) {
        const fileExt = file.name.substring(file.name.lastIndexOf('.'))
        const list = this.accept.split(',')
        const hasAccept = !!list.find(item => item === fileExt)
        if (!hasAccept) {
          this.$message({
            message: '上传文件类型必须为' + this.accept,
            type: 'error'
          })
          return false
        }
      }
      if (this.alwaysCrop) {
        this.option.img = URL.createObjectURL(file)
        this.dialogVisible = true
        return false
      }
      if (this.imgWidth || this.imgHeight || this.minWidth || this.minHeight || this.isSquare) {
        isSize = this.sizeCheck(file)
      }
      return isFileSize && hasAccept && isSize
    },
    // 覆盖默认上传行为-正面
    httpRequest_front(params) {
      const fd = new FormData()
      fd.append('file', params.file)
      upload(fd).then(res => {
        if (res) {
          this.isChange = true
          this.idcard_front = res.image
        }
      })
    },
    // 覆盖默认上传行为-反面
    httpRequest_back(params) {
      const fd = new FormData()
      fd.append('file', params.file)
      upload(fd).then(res => {
        if (res) {
          this.isChange = true
          this.idcard_back = res.image
        }
      })
    },
    // 关闭预览
    closeFront() {
      this.showFront = false
    },
    closeBack() {
      this.showBack = false
    },
    changeMemberMobile() {
      const data = {
        order_uid: this.editItem.uid,
        member_mobile: this.mobile
      }
      changeMemberMobile(data).then(
        res => {
          this.$message.success('修改成功')
          this.$emit('getList', true)
          this.dialogVisible = false
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.idcard_wrap {
  border: 1px solid #eeeeee;
  & > div:first-child {
    border-right: 1px solid #eeeeee;
  }
}
/deep/ .bbb #n1 {
  width: 120px;
}
.img_header {
  height: 40px;
  line-height: 40px;
  background: #f5f5f5;
  color: #666666;
  font-size: 16px;
}
.img_wrap {
  width: 250px;
  height: 150px;
  margin: 20px auto;
  background-color: #8f8f8f;
}
.img_footer {
  height: 40px;
  line-height: 40px;
  border-top: 1px solid #eeeeee;
}
</style>
